package com.tapstitch.scm.common.enums;

import org.apache.commons.lang3.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Content-Type对照枚举类
 *
 * @author liyang
 * @date 2019-08-21
 */
public enum ContentTypeEnum {
    IMG(".img", "application/x-img"),
    JPEG(".jpeg", "image/jpeg"),
    JPG(".jpg", "image/jpg"),
    JPE(".jpe", "image/jpeg"),
    GIF(".gif", "image/gif"),
    PNG(".png", "image/png"),
    WEBP(".webp", "image/webp"),
    SVG(".svg", "image/svg+xml"),
    PDF(".pdf", "application/pdf"),
    DOC(".doc", "application/msword"),
    XLS(".xls", "application/vnd.ms-excel"),
    XLSX(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),

    JSON(".json", "application/json"),

    ZIP(".zip", "application/zip"),
    CSV(".csv", "text/csv; charset=utf-8"),

    ;

    private String suffix;

    private String contentType;


    ContentTypeEnum(String suffix, String contentType) {
        this.suffix = suffix.toLowerCase();
        this.contentType = contentType;
    }

    /**
     * 根据文件后缀获取，请带点，如：image/jpeg
     *
     * @return
     */
    public static ContentTypeEnum getByContentType(String contentType) {
        if (StringUtils.isEmpty(contentType)) {
            return null;
        }
        for (ContentTypeEnum v : values()) {
            if (v.contentType.equals(contentType)) {
                return v;
            }
        }
        return null;
    }
    /**
     * 根据contentType获取，请带点，如：.pdf
     *
     * @return
     */
    public static ContentTypeEnum getBySuffix(String suffix) {
        if (suffix == null) {
            return null;
        }
        suffix = suffix.trim().toLowerCase();
        for (ContentTypeEnum v : values()) {
            if (v.suffix.equals(suffix)) {
                return v;
            }
        }
        return null;
    }

    /**
     * 根据文件名获取，请带点，如：XXX.pdf
     *
     * @return
     */
    public static ContentTypeEnum getByFileName(String fileName) {
        return getByFileName(fileName, null);
    }

    public static ContentTypeEnum getByFileName(String fileName, ContentTypeEnum defaultType) {
        if (fileName == null) {
            return defaultType;
        }
        int lastIndexOf = fileName.lastIndexOf(".");
        if (lastIndexOf == -1) {
            return defaultType;
        }
        ContentTypeEnum contentType = getBySuffix(fileName.substring(lastIndexOf).toLowerCase());
        return contentType == null ? defaultType : contentType;
    }

    public static ContentTypeEnum getByUrl(String url) {
        return getByUrl(url, null);
    }

    public static ContentTypeEnum getByUrl(String url, ContentTypeEnum defaultType) {
        if (url == null) {
            return defaultType;
        }
        String path;
        try {
            path = new URL(url).getPath();
        } catch (MalformedURLException e) {
            return defaultType;
        }
        return getByFileName(path, defaultType);
    }

    public String getSuffix() {
        return suffix;
    }


    public String getContentType() {
        return contentType;
    }

}
